using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace jivemessenger
{
    [BroadcastReceiver(Permission = "com.google.android.c2dm.permission.SEND")]
    [IntentFilter(new string[] { "com.google.android.c2dm.intent.RECEIVE" }, Categories = new string[] { "jive.messenger" })]
    [IntentFilter(new string[] { "com.google.android.c2dm.intent.REGISTRATION" }, Categories = new string[] { "jive.messenger" })]
    [IntentFilter(new string[] { "com.google.android.gcm.intent.RETRY" }, Categories = new string[] { "jive.messenger" })]
    public class JiveGCMBroadcastReceiver : BroadcastReceiver
    {
        private const string TAG = "PushHandlerBroadcastReceiver";

        public override void OnReceive(Context context, Intent intent)
        {
            intent.PutExtra("messenger", new Messenger(new JiveGCMHandler()));
            JiveGCMIntentService.RunIntentInService(context, intent);
            SetResult(Result.Ok, null, null);
        }
    }
    [BroadcastReceiver]
    [IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
    public class JiveGCMBootReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            intent.PutExtra("messenger", new Messenger(new JiveGCMHandler()));
            JiveGCMIntentService.RunIntentInService(context, intent);
            SetResult(Result.Ok, null, null);
        }
    }

    class JiveGCMHandler : Handler
    {
        public override void HandleMessage(Message msg)
        {
            Bundle reply = msg.Data;
            string regID = reply.GetString("registrationId");
            string error = reply.GetString("error");
            string unreg = reply.GetString("unregistration");
            if (!String.IsNullOrEmpty(regID))
                StaticService.Binder.Service.Connection.SendStatusMessage(JmShared.MessageType.PushUri, regID);
        }
    }

    public static class JiveGCM
    {
        public static void Register()
        {
            string senders = "755201211625";
            Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
            intent.SetPackage("com.google.android.gsf");
            intent.PutExtra("app", PendingIntent.GetBroadcast(StaticSettings.GlobalContext, 0, new Intent(), 0));
            intent.PutExtra("sender", senders);
            StaticSettings.GlobalContext.StartService(intent);
            
        }

        public static void UnRegister()
        {
            Intent intent = new Intent("com.google.android.c2dm.intent.UNREGISTER");
            intent.PutExtra("app", PendingIntent.GetBroadcast(StaticSettings.GlobalContext, 0, new Intent(), 0));
            StaticSettings.GlobalContext.StartService(intent);
        }
    }
}